from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_model_utils.models import StatMixin, PageMixin
from tagging.fields import TagField
from tagging.models import Tag
from django.contrib.auth.models import User
from gironimo.category.models import Category


def get_image_path(instance, filename):
    '''
    Create dynamic image upload path for every gallery.
    '''
    return 'gallery/%s/%s' % (str(instance.album.slug), filename)


class Album(StatMixin, PageMixin, models.Model):
    '''
        Model for an gallery
    '''
    name = models.CharField(max_length=150, verbose_name=_('Name'))
    slug = models.SlugField(max_length=150, unique=True)
    content = models.TextField(verbose_name=_('Beschreibung'), blank=True, null=True)
    
    user = models.ForeignKey(User, related_name='user')
    category = models.ForeignKey(Category, related_name='category', verbose_name=_('Kategorie'))
    
    def __unicode__(self):
        return self.name
    
    @models.permalink
    def get_absolute_url(self):
        return 'gallery_detail', (), {'slug': self.slug}
    
    class Meta:
        verbose_name = _(u'Fotoalbum')
        verbose_name_plural = _(u'Fotoalben')


class Picture(StatMixin, models.Model):
    '''
        Model for images. Every Image must have a gallery.
    '''
    album = models.ForeignKey(Album, related_name='album')
    
    image = models.ImageField(max_length=180, upload_to=get_image_path, verbose_name=_('Bild'))
    content = models.TextField(verbose_name=_('Beschreibung'), blank=True, null=True)
    tags = TagField()
    
    def set_tags(self, tags):
        Tag.objects.update_tags(self, tags)
    
    def get_tags(self, tags):
        return Tag.objects.get_for_object(self)
    
    def __unicode__(self):
        return '%s Picture #%d'% (self.album.name, self.id)
    
    class Meta:
        verbose_name = _(u'Foto')
        verbose_name_plural = _(u'Fotos')

# Load imagequery
import gironimo.gallery.image_formats

